# Maintainer: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=imagecodecs
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2022.12.24
pkgrel=1
pkgdesc="Image transformation, compression, and decompression codecs (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/cgohlke/imagecodecs'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-blosc"
         "${MINGW_PACKAGE_PREFIX}-blosc2"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-cfitsio"
         "${MINGW_PACKAGE_PREFIX}-charls"
         "${MINGW_PACKAGE_PREFIX}-jxrlib"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-lerc"
         "${MINGW_PACKAGE_PREFIX}-libaec"
         "${MINGW_PACKAGE_PREFIX}-libavif"
         "${MINGW_PACKAGE_PREFIX}-libdeflate"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libjxl"
         "${MINGW_PACKAGE_PREFIX}-libmng"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-zopfli")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "0001-deflate.patch")
sha256sums=('c5282a808786dadb8e3ee238bb213b2e065574f1bb0f4b51e8bf93747e0babb3'
            '7c8e664be507b8aadca548bf2f2d56ba7c6bbdfcebc0de1bc89376536f870cba')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}"/0001-deflate.patch

  cd "${srcdir}"
  rm -rf python-build-${MSYSTEM} || true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  msg "Python test for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py check --strict
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
  install -Dm644 3rdparty/bitshuffle/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-bitshuffle"
  install -Dm644 3rdparty/cfitsio/License.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-cfitsio"
  install -Dm644 3rdparty/liblj92/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-liblj92"
  install -Dm644 3rdparty/liblzf/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-liblzf"
  install -Dm644 3rdparty/libspng/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-libspng"
  install -Dm644 3rdparty/postgresql/COPYRIGHT "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-postgresql"
  install -Dm644 imagecodecs/licenses/LICENSE-qoi "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE-qoi"

  # remove shebang line
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    sed -e '1 { s/^#!.*$// }' -i ${_f}
  done
}
